package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.util.AssertUtil;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirAlertZoneMapper;
import zhongbo.java.air.dictionary.model.AirAlertZone;
import zhongbo.java.air.dictionary.service.IAirAlertZoneService;

import java.util.Date;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月01日 星期二 08:20:35
 */
@Service
@Slf4j
public class AirAlertZoneServiceImpl implements IAirAlertZoneService<AirAlertZone> {

    @Autowired
    private AirAlertZoneMapper airAlertZoneMapper;

    @Override
    public AirAlertZone add(AirAlertZone airAlertZone) {
        validAirAlertZone(airAlertZone);
        AssertUtil.isNull(airAlertZone.getId(), "实体id不能填写");
        int countByZoneCode = airAlertZoneMapper.selectCountByZoneCode(airAlertZone.getZoneCode());
        if (countByZoneCode > 0) {
            throw new RuntimeException("区域业务编号重复");
        }

        airAlertZone.setCreateTime(new Date());
        airAlertZone.setUpdateTime(new Date());
        int insert = airAlertZoneMapper.insert(airAlertZone);
        if (insert > 0) {
            return airAlertZone;
        }
        throw new RuntimeException("新增实体失败");
    }

    /**
     * 通用验证实体信息
     */
    private void validAirAlertZone(AirAlertZone airAlertZone) {
        if (airAlertZone == null) {
            throw new RuntimeException("实体信息不能为空");
        }
        AssertUtil.maxLength(airAlertZone.getZoneCode(), 20, "区域业务编号不能超过20位");
        AssertUtil.notBlank(airAlertZone.getZoneCode(), "区域业务编号不能为空");
        AssertUtil.maxLength(airAlertZone.getZoneType(), 20, "区域类型不能超过20位");
        AssertUtil.notBlank(airAlertZone.getZoneType(), "区域类型不能为空");
        AssertUtil.notNull(airAlertZone.getEffectiveStart(), "生效开始时间不能为空");
        AssertUtil.notNull(airAlertZone.getZoneType(), "区域类型不能为空");
        AssertUtil.notNull(airAlertZone.getVertices(), "多边形顶点坐标不能为空");
    }

    @Override
    public String delete(String airAlertZoneId) {
        if (StrUtil.isBlank(airAlertZoneId)) {
            throw new RuntimeException("实体id不能为空");
        }
        int delete = airAlertZoneMapper.deleteById(airAlertZoneId);
        if (delete > 0) {
            return airAlertZoneId;
        }
        throw new RuntimeException("删除实体失败");
    }

    @Override
    public AirAlertZone edit(AirAlertZone edit) {
        validAirAlertZone(edit);
        String airAlertZoneId = edit.getId();
        if (StrUtil.isBlank(airAlertZoneId)) {
            throw new RuntimeException("实体id不能为空");
        }
        AirAlertZone one = findOne(airAlertZoneId);
        if (one == null) {
            throw new RuntimeException("实体不存在");
        }
        edit.setUpdateTime(new Date());
        int update = airAlertZoneMapper.updateById(edit);
        if (update > 0) {
            return edit;
        }
        throw new RuntimeException("修改实体失败");
    }

    @Override
    public AirAlertZone findOne(String airAlertZoneId) {
        if (StrUtil.isBlank(airAlertZoneId)) {
            throw new RuntimeException("实体id不能为空");
        }
        return airAlertZoneMapper.selectById(airAlertZoneId);
    }

    @Override
    public PageVo<AirAlertZone> findPage(Integer page, Integer pageSize, String keyword, String fields, String oderByField, Boolean orderByAsc) {
        QueryWrapper<AirAlertZone> queryWrapper = new QueryWrapper<>();
        // 检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            // 检测是正序还是倒序
            boolean isOrderByAsc = Boolean.TRUE.equals(orderByAsc);
            if (isOrderByAsc) {
                // 结果正序排列
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(oderByField));
            } else {
                // 否则结果倒序排列
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(oderByField));
            }
        }
        // 支持指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        Page<AirAlertZone> pageParam = new Page<>(page, pageSize);
        Page<AirAlertZone> airAlertZonePage = airAlertZoneMapper.selectPage(pageParam, queryWrapper);

        return new PageVo<>(airAlertZonePage);
    }

    private void validPassWord(String passWord) {
        if (StrUtil.isBlank(passWord)) {
            throw new RuntimeException("密码不能为空");
        }
    }

    private void validAirAlertZoneName(String airAlertZoneName) {
        if (StrUtil.isBlank(airAlertZoneName)) {
            throw new RuntimeException("实体名不能为空");
        }
    }


}
